package com.example.mycarcalling.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.example.lib_cache.service.ApiService;
import com.example.lib_cache.viewmodel.PasswordEditActivityViewModel;
import com.example.mycarcalling.R;
import com.example.mycarcalling.databinding.ActivityEditPasswordBinding;

import javax.inject.Inject;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class PasswordEditActivity extends AppCompatActivity {

    private final static String TAG = PasswordEditActivity.class.getSimpleName();

    private ActivityEditPasswordBinding activityEditPasswordBinding = null;
    private PasswordEditActivityViewModel passwordEditActivityViewModel = null;

    private Context context = null;

    @Inject
    ApiService apiService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityEditPasswordBinding = DataBindingUtil
                .setContentView(this, R.layout.activity_edit_password);
        context = this;
        initView();
        initData();
        initListener();
        initObserve();
    }

    private void initView(){
        setTitle("修改密码");
    }
    private void initData(){
        //创建数据源
        //创建一个ViewModel的工厂
        ViewModelProvider.Factory factory = ViewModelProvider.
                AndroidViewModelFactory.getInstance(getApplication());
        //创建ViewModel的管理对象
        ViewModelProvider viewModelProvider = new ViewModelProvider(this,factory);
        //生产ViewModel
        passwordEditActivityViewModel = viewModelProvider.get(PasswordEditActivityViewModel.class);
    }
    private void initListener(){
        //旧密码
        activityEditPasswordBinding.tietEtOldPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String str = activityEditPasswordBinding.tietEtOldPassword.getText().toString();
                if(TextUtils.isEmpty(str)&&"".equals(str)){
                    activityEditPasswordBinding.tietEtOldPassword.setError("请输入密码");
                }else {
                    if(str.length()>=6&&str.length()<=20){
                        passwordEditActivityViewModel.setOldPassword(str);
                    }else {
                        activityEditPasswordBinding.tietEtOldPassword.setError("请输入6-12位数的英文字母与数字的组合");
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                String str = activityEditPasswordBinding.tietEtOldPassword.getText().toString();
                if(TextUtils.isEmpty(str)&&"".equals(str)){
                    activityEditPasswordBinding.tietEtOldPassword.setError("请输入密码");
                }else {
                    if(str.length()>=6&&str.length()<=20){
                        passwordEditActivityViewModel.setOldPassword(str);
                    }else {
                        activityEditPasswordBinding.tietEtOldPassword.setError("请输入6-12位数的英文字母与数字的组合");
                    }
                }
            }
        });

        //新密码
        activityEditPasswordBinding.tietEtNewPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String str = activityEditPasswordBinding.tietEtNewPassword.getText().toString();
                if(TextUtils.isEmpty(str)&&"".equals(str)){
                    activityEditPasswordBinding.tietEtNewPassword.setError("请输入密码");
                }else {
                    if(str.length()>=6&&str.length()<=20){
                        if(str.equals(
                                activityEditPasswordBinding.tietEtConfirmPassword.getText().toString())){

                        }else {
                            activityEditPasswordBinding.tietEtNewPassword.setError("两次新密码不一致");
                        }
                    }
                    else {
                        activityEditPasswordBinding.tietEtNewPassword.setError("请输入6-12位数的英文字母与数字的组合");
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                String str = activityEditPasswordBinding.tietEtNewPassword.getText().toString();
                if(TextUtils.isEmpty(str)&&"".equals(str)){
                    activityEditPasswordBinding.tietEtNewPassword.setError("请输入密码");
                }else {
                    if(str.length()>=6&&str.length()<=20){
                        if(str.equals(
                                activityEditPasswordBinding.tietEtConfirmPassword.getText().toString())){

                        }else {
                            activityEditPasswordBinding.tietEtNewPassword.setError("两次新密码不一致");
                        }
                    }
                    else {
                        activityEditPasswordBinding.tietEtNewPassword.setError("请输入6-12位数的英文字母与数字的组合");
                    }
                }
            }
        });
        //确认新密码
        activityEditPasswordBinding.tietEtConfirmPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String str = activityEditPasswordBinding.tietEtConfirmPassword.getText().toString();
                if(TextUtils.isEmpty(str)&&"".equals(str)){
                    activityEditPasswordBinding.tietEtConfirmPassword.setError("请输入密码");
                }else {
                    if(str.length()>=6&&str.length()<=20){
                        if(str.equals(
                                activityEditPasswordBinding.tietEtNewPassword.getText().toString())){
                            passwordEditActivityViewModel.setConfirmPassword(str);
                        }else {
                            activityEditPasswordBinding.tietEtConfirmPassword.setError("两次新密码不一致");
                        }
                    }
                    else {
                        activityEditPasswordBinding.tietEtConfirmPassword.setError("请输入6-12位数的英文字母与数字的组合");
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                String str = activityEditPasswordBinding.tietEtConfirmPassword.getText().toString();
                if(TextUtils.isEmpty(str)&&"".equals(str)){
                    activityEditPasswordBinding.tietEtConfirmPassword.setError("请输入密码");
                }else {
                    if(str.length()>=6&&str.length()<=20){
                        if(str.equals(
                                activityEditPasswordBinding.tietEtNewPassword.getText().toString())){
                            passwordEditActivityViewModel.setConfirmPassword(str);
                        }else {
                            activityEditPasswordBinding.tietEtConfirmPassword.setError("两次新密码不一致");
                        }
                    }
                    else {
                        activityEditPasswordBinding.tietEtConfirmPassword.setError("请输入6-12位数的英文字母与数字的组合");
                    }
                }
            }
        });

        //发起请求
        activityEditPasswordBinding.btnConfirmPasswordEt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG,"发起修改密码请求");
                passwordEditActivityViewModel.requestUpdatePassWord(apiService,context);
            }
        });
    }
    private void initObserve(){
        passwordEditActivityViewModel.getResult().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if(aBoolean){
                    Toast.makeText(context, "已更新密码，请注意重新登录", Toast.LENGTH_SHORT).show();
                    return;
                }
                Toast.makeText(context, "原密码存在错误", Toast.LENGTH_SHORT).show();
            }
        });
    }

}
